package models

import (
	"beegoProject/global"
	"fmt"
	"github.com/astaxie/beego/orm"
	"time"
)

// 定义结构体
// User -> name 姓名  nick_name 昵称 sex 性别 mobile 手机号 password 密码 created_time 注册时间

type User struct {
	Id          int
	Name        string
	NickName    string
	Sex         int
	Age         int
	Mobile      string
	Password    string
	CreatedTime time.Time //time标准库
}

func Register(name string, nicke_name string, sex int, mobile string, password string) int64 {
	//model层进行数据处理
	user := User{
		Name:        name,
		NickName:    nicke_name,
		Sex:         sex,
		Mobile:      mobile,
		Password:    password,
		CreatedTime: time.Now(),
	}
	//将数据入库
	o := orm.NewOrm()
	result, err := o.Insert(&user)
	if err != nil {
		fmt.Println("error inserting user")
	}
	return result
}

func CheckMobile(mobile string) bool {
	//对数据库中的mobile进行查询
	o := orm.NewOrm()
	//查询条件
	var user User
	user.Mobile = mobile
	//查询单条数据
	err := o.Read(&user, "Mobile")
	fmt.Println(err)
	if user.Id > 0 {
		return true
	}
	return false

}

// 通过手机号获取用户信息
func GetUserInfoByMobile(mobile string) User {
	o := orm.NewOrm()
	//查询条件
	var user User
	user.Mobile = mobile
	//查询单条数据
	err := o.Read(&user, "Mobile")
	if err != nil {
		panic("Mobile not found")
	}
	return user
}

// 通过userId获取用户信息
func GetUserInfoByUserId(userId int) User {
	o := orm.NewOrm()
	//查询条件
	var user User
	user.Id = userId
	//查询单条数据
	err := o.Read(&user, "Id")
	if err != nil {
		panic("user not found")
	}
	return user
}

// 修改手机号
func UpdateMobile(user User, newMobile string) User {
	o := orm.NewOrm()
	//修改条件
	user.Mobile = newMobile
	//修改数据
	_, err := o.Update(&user)
	if err != nil {
		panic("Mobile not found")
	}
	return user
}

// 修改用户信息
func UpdataUserInfo(name string, user User) User {
	o := orm.NewOrm()
	user.Name = name
	_, err := o.Update(&user)
	if err != nil {
		panic("update user error")
	}
	return user
}

// 通过UserId删除某个用户
func DeleteUserByUserId(userId int) {
	o := orm.NewOrm()
	var user User
	user.Id = userId
	//删除操作
	_, err := o.Delete(&user)
	if err != nil {
		panic("delete user failed")
	}
}

func RegisterByGorm(name string, nicke_name string, sex int, mobile string, password string) int64 {
	user := User{
		Name:        name,
		NickName:    nicke_name,
		Sex:         sex,
		Mobile:      mobile,
		Password:    password,
		CreatedTime: time.Now(),
	}
	//入库
	result := global.Db.Create(&user)
	if result.Error != nil {
		panic(result.Error)
	}
	return result.RowsAffected
}

func GetUserInfoByMobileByGorm(mobile string) User {
	//查询条件
	var user User
	//查询单条数据
	result := global.Db.Where("mobile =?", mobile).First(&user)
	if result.Error != nil {
		panic("Mobile not found")
	}
	return user
}

func UpdateMobileByGorm(user User, mobile string) User {
	//修改条件
	user.Mobile = mobile
	//修改数据  1. 通过save函数修改
	global.Db.Save(&user)
	//2.通过update函数修改
	//global.Db.Model(&user).Update("mobile", mobile)
	return user
}

func DeleteUserByUserIdAndGorm(userId int) {
	var user User
	user.Id = userId
	//删除操作
	result := global.Db.Delete(&user)
	if result.Error != nil {
		panic("delete user failed")
	}

}
